<?php

class Admin_Model_Manages{

	private $manager;
	
	public function __construct(){
		$this->manager = new Admin_Model_DbTable_Quanlys();
	}

	function selectManager($where =null,$order = NULL,$limit =NULL) {
		$dbAdapter = Zend_Db_Table::getDefaultAdapter();
		$dbSelect = new Zend_Db_Select($dbAdapter);
		$dbSelect->from(array('ql'=>'quanly'),'*')
				->joinLeft(
						array("u"=>"users"), 
						"u.username = ql.username",
						array('quyen','hoDem','ten','email','ngaySinh')
				)
				->joinLeft(array('l'=>'lop'), 'l.id = ql.idLop',array('*'))
				->joinLeft(array('mh'=>'monhoc'), 'mh.id = ql.idMonHoc',array('tenMonHoc'));
		
		if ($where != NULL) $dbSelect->where($where);
		if ($order != NULL) $dbSelect->order($order);
		if ($limit != NULL) $dbSelect->limit($limit);
		
		return $dbSelect;
	}
	
	function addManage($username,$idLop,$idMonHoc) {
		$data = array(
			'username'	=>	$username,
			'idLop'		=>	$idLop,
			'idMonHoc'	=>	$idMonHoc
		);
		$this->manager->insert($data);
	}
	
	function update($data,$where) {
		$this->manager->update($data, $where);
	}
	
	function deleteManage($where) {
		$this->manager->delete($where);
	}
	
/**
	 * 
	 * Lay du lieu cua tat ca cac cot trong array column
	 * @param unknown_type $colArr: danh sach cac cot
	 */
	public function fetchCol($colArr,$where=NULL){
		$sql = "SELECT ";
		for($i =0; $i<count($colArr); $i++){
			if($i != count($colArr)-1)
				$sql .= " ".$colArr[$i]."," ;
			else 
				$sql .= " ".$colArr[$i];
		}
		$sql .=" FROM quanly ";
		
		if ($where!=NULL){
			$sql .= " WHERE ".$where;	
		}
		return $this->manager
					->getAdapter()
					->fetchCol($sql);
	}
	
}

